package com.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.base.ResultSetHandler;
import com.dao.UserDAO;
import com.util.db.JdbcTemplate;
import com.vo.User;

public class UserDAOImpl implements UserDAO {
	private JdbcTemplate jdbcTemplate;

	public UserDAOImpl() {
		jdbcTemplate = new JdbcTemplate();
	}
	
	@Override
	public User find(String username, String password) throws SQLException {
		String sql = "SELECT * FROM Login WHERE username=? AND password=?";
		User user = (User)jdbcTemplate.query(sql, new ResultSetHandler() {
			
			@Override
			public Object doHandler(ResultSet rs) throws SQLException {
				User user = null;
				
				if(rs.next()) {
					user = new User();
					user.setUsername(rs.getString("username"));
					user.setPassword(rs.getString("password"));
				}
				
				return user;
			}
		}, username, password);
		
		return user;
	}

	@Override
	public boolean check(String username, String password) throws SQLException {
		User user = null;
		
		user = find(username, password);
		
		if(user != null) {
			return true;
		}
		
		return false;
	}

}
